﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CommonUtil
{
    public class DataTimeUtil
    {
        private static TimeSpan ts;
        public static TimeSpan TimeSpanDiff(DateTime dtBegin, DateTime dtEnd)
        {
            TimeSpan tsBegin = new TimeSpan(dtBegin.Ticks);
            TimeSpan tsEnd = new TimeSpan(dtEnd.Ticks);
            TimeSpan ts = tsBegin.Subtract(tsEnd).Duration();
            return ts;
        }

        #region 计算两个DateTime的总时间间隔
        /// <summary>
        /// 总时间间隔：毫秒
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public int MillisecondsDiff(DateTime dtBegin, DateTime dtEnd)
        {
            ts = TimeSpanDiff(dtBegin, dtEnd);
            return Convert.ToInt32(ts.TotalMilliseconds);
        }

        /// <summary>
        /// 总时间间隔：秒
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public int SecondsDiff(DateTime dtBegin, DateTime dtEnd)
        {
            ts = TimeSpanDiff(dtBegin, dtEnd);
            return Convert.ToInt32(ts.TotalSeconds);
        }

        /// <summary>
        /// 总时间间隔：分
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public int MinutesDiff(DateTime dtBegin, DateTime dtEnd)
        {
            ts = TimeSpanDiff(dtBegin, dtEnd);
            return Convert.ToInt32(ts.TotalMinutes);
        }

        /// <summary>
        /// 总时间间隔：时
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public int HoursDiff(DateTime dtBegin, DateTime dtEnd)
        {
            ts = TimeSpanDiff(dtBegin, dtEnd);
            return Convert.ToInt32(ts.TotalHours);
        }

        /// <summary>
        /// 总时间间隔：天
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public int DaysDiff(DateTime dtBegin, DateTime dtEnd)
        {
            ts = TimeSpanDiff(dtBegin, dtEnd);
            return Convert.ToInt32(ts.TotalDays);
        }
        #endregion 计算两个DateTime的总时间间隔

        public static string DateTimeDiff(DateTime dtBegin, DateTime dtEnd)
        {
            ts = TimeSpanDiff(dtBegin, dtEnd);
            string dateDiff = ts.Days.ToString() + "天"
                + ts.Hours.ToString() + "小时"
                + ts.Minutes.ToString() + "分钟"
                + ts.Seconds.ToString() + "秒";
            return dateDiff;
        }

        /// <summary>
        /// 时间相加
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <returns>DateTime</returns>
        public static DateTime TimeSum(DateTime dtBegin, DateTime dtEnd)
        {
            dtBegin = dtBegin.AddHours(dtEnd.Hour);
            dtBegin = dtBegin.AddMinutes(dtEnd.Minute);
            dtBegin = dtBegin.AddSeconds(dtEnd.Second);
            //dtBegin = dtBegin.AddMilliseconds(dtEnd.Millisecond);
            return dtBegin;
        }

        /// <summary>
        /// 日期相加
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <returns>DateTime</returns>
        public static DateTime DateSum(DateTime dtBegin, DateTime dtEnd)
        {
            dtBegin = dtBegin.AddYears(dtEnd.Year);
            dtBegin = dtBegin.AddMonths(dtEnd.Month);
            dtBegin = dtBegin.AddDays(dtEnd.Day);
            return dtBegin;
        }

        /// <summary>
        /// 取某个月的最后一天
        /// </summary>
        /// <param name="dtBegin">为NULL则取当前时间的</param>
        /// <returns>DateTime</returns>
        public static DateTime GetLastDateForMonth(DateTime dtBegin)
        {
            int dtYear, dtMonth;
            DateTime dt;
            if (dtBegin == null)
                dtBegin = DateTime.Now; //DateTime.Now.AddMonths(1);
            dtYear = dtBegin.Year;
            dtMonth = dtBegin.Month;
            //法一：算出该月多少天，年+月+加上多少天
            int MonthCount = DateTime.DaysInMonth(dtYear, dtMonth);
            dt = Convert.ToDateTime(dtYear.ToString() + "-" + dtMonth.ToString() + "-" + MonthCount);
            //法二：取出下月的第一天减去一天
            //dt = Convert.ToDateTime(dtYear.ToString() + "-" + dtMonth.ToString() + "-" + "1").AddDays(-1);
            return dt;
        }
        /// <summary>
        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        public static DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }

        /// <summary>
        /// 取得上个月第一天
        /// </summary>
        /// <param name="datetime">要取得上个月第一天的当前时间</param>
        /// <returns></returns>
        public static DateTime FirstDayOfPreviousMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
        }

        /// <summary>
        /// 时间格式转长字符串，返回格式为yyy-MM-dd hh:mm:ss
        /// </summary>
        /// <param name="datetime">需要转换的时间</param>
        /// <returns>返回格式为yyy-MM-dd hh:mm:ss</returns>
        public static string DatetimeToLongString(DateTime datetime)
        {
            return datetime.ToString("yyyy-MM-dd hh:mm:ss");
        }
        /// <summary>
        /// 时间格式中日期部分转短字符串(日期），返回格式为yyy-MM-dd
        /// </summary>
        /// <param name="datetime">需要转换的时间</param>
        /// <returns>返回格式为yyy-MM-dd</returns>
        public static string DatetimeToShortString(DateTime datetime)
        {
            return datetime.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 时间格式中时间部分转字符串，返回格式hh:mm:ss
        /// </summary>
        /// <param name="datetime">需要转换的时间</param>
        /// <returns>返回格式hh:mm:ss</returns>
        public static string DateTimetoStringTime(DateTime datetime)
        {
            return datetime.ToLongTimeString();
        }
    }
}
